/**
 * 将多个表除时间那一列外合并成同一个表
 * 实际情况是表与表之间的日期数据可能对应不上，如果当前
 * [
 *  ['日期', 'A指标','B指标'],
 *  ['10/1', '1,1','1,2'],
 *  ['10/2', '2,1','2,2'],
 * ] 和
 * [
 *  ['日期', 'C指标'],
 *  ['10/2', '2,3'],
 *  ['10/3', '3,3'],
 * ]
 * 输出
 * [
 *  ['日期', 'A指标','B指标', 'C指标'],
 *  ['10/1', '1,1','1,2',''],
 *  ['10/2', '2,1','2,2','2,3'],
 *  ['10/3', '','','3,3'],
 * ]
 * @param {*} sheets 
 */
function mergeSheets(sheets) {
  const _sheets = [...sheets];
  // 将日期作为key，保证每个表的同一日期都为同一行数据
  const map = new Map();
  let colCount = 0;
  _sheets.map((sheet, sheetIndex) => {
    let rowIndex = 1;
    while (rowIndex < sheet.length) {
      const row = sheet[rowIndex];
      if (!row.length || row.length < 2) {
        console.error(sheetIndex, rowIndex, '该行数据缺失');
      }
      const falseCol = row[0];

      // 转换为cell保存
      const cells = row.slice(1).map((value, valueIndex) => {
        let curSheetIndex = sheetIndex;
        let colStart = valueIndex;

        while (curSheetIndex > 0) {
          colStart += Math.max(0, sheets[curSheetIndex - 1][0].length - 1);
          curSheetIndex--;
        }
        // 加一跳过日期那一列
        const colNumber = colStart + 1;
        const cell = Object.create(null);
        cell.colIndex = colNumber, cell.value = value;
        return cell;
      })
      if (!map.has(falseCol)) {
        map.set(falseCol, cells)
      } else {
        map.set(falseCol, map.get(falseCol).concat(cells))
      }
      rowIndex++;
    }
  })

    // 生成表头
  const sheetHeader = sheets.reduce((p, c) => p.concat(c[0].slice(1)), ['日期'])
  // 根据日期排序
  const ascKeys = [...map.keys()].sort((a, b) => a - b);

  const noYearMap = mergeDate(ascKeys, map);
  const noYearDate = [...noYearMap.keys()];
  // 创建一个新表格
  const newSheet = createNewSheet(noYearDate, noYearMap, sheetHeader);

  // console.table(newSheet);
  return newSheet;
}

/**
 * 将每一年同一个日期的数据合并在同一行
 * @param {*} ascKeys 已排序过的时间戳
 * @param {*} map 所有日期和该日期同一行的数据的key-value对
 */
function mergeDate(ascKeys, map) {
  const dateMap = new Map();
  ascKeys.forEach(date => {;

    if (!dateMap.has(date)) {
      dateMap.set(date, [...map.get(date)])
    } else {
      dateMap.set(date, dateMap.get(dateString).concat(map.get(timestamp)))
    }
  })

  return dateMap;
}

/**
 * 生成一个新表格
 * @param {*} ascKeys 
 * @param {*} map 
 */
function createNewSheet(ascKeys, map, sheetHeader) {
  // 生成一个行数为map.size, 列数为各表之和的空字符串表格
  const newSheet = [sheetHeader]
    .concat(Array.from({ length: map.size }, () => Array(sheetHeader.length).fill('')));

  ascKeys.forEach((time, index) => {
    const cells = map.get(time);
    newSheet[index + 1][0] = time;
    cells.forEach(cell => {
      const { colIndex, value } = cell;
      newSheet[index + 1][colIndex] = value;
    })
  });
  return newSheet;
}

/**
 * 合并散点图的表格
 * @param {*} sheets 
 */
function mergeSheetsScatter(sheets) {
  const map = new Map();
  let rowIndex = 1;
  const max = Math.max(...sheets.map(sheet => sheet.length));
  while (rowIndex < max) {
    sheets.forEach((sheet, sheetIndex) => {
      const row = sheet[rowIndex];
      if(row){
        const date = row[0];
        const data = row.slice(1);
  
        if(!map.has(date)){
          let mergeRow = data.map(value => {
            let cell = new Array(sheets.length).fill(null);
            cell[sheetIndex] = value;
            return cell;
          });
          map.set(date, mergeRow)
        } else {
          const mergeRow = map.get(date);
          data.forEach((value, index) => {
            mergeRow[index][sheetIndex] = value;
          })
        }
      }
    })
    rowIndex++;
  }
  // 根据日期排序
  const ascKeys = [...map.keys()].sort((a, b) => a - b);
  const sheetHeader = sheets[0][0];
  const newSheet = [sheetHeader];
  ascKeys.forEach(date => {
    const row = map.get(date);
    newSheet.push([date].concat(row))
  })
  return newSheet;
}

export default {
  mergeSheets,
  mergeSheetsScatter,
}